package com.squareup.picasso;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

public final class LruCache implements Cache {
    private LinkedHashMap<String, Bitmap> map;
    private final int maxSize;
    private int size;
    private int putCount;
    private int evictionCount;
    private int hitCount;
    private int missCount;

    public LruCache(Context arg4) {
        this(Utils.calculateMemoryCacheSize(arg4));
        Log.e("Picasso-LruCache", "MaxSize: " + this.maxSize);
    }

    private LruCache(int maxSize) {
        if (maxSize <= 0) {
            throw new IllegalArgumentException("Max size must be positive.");
        }

        this.maxSize = maxSize;
        this.map = new LinkedHashMap<String, Bitmap>(0, 0.75f, true);
    }

    public final void trimToSize(int maxSize) {
        while (true) {
            synchronized (this) {
                if (this.size >= 0 && (!this.map.isEmpty() || this.size == 0)) {
                    if (this.size > maxSize && !this.map.isEmpty()) {
                        Map.Entry<String, Bitmap> toEvict = this.map.entrySet().iterator().next();
                        String key = toEvict.getKey();
                        Bitmap value = toEvict.getValue();
                        this.map.remove(key);
                        this.size -= Utils.getBitmapBytes(value);
                        ++this.evictionCount;
                        continue;
                    }
                    return;
                }
                throw new IllegalStateException(this.getClass().getName() + ".sizeOf() is reporting inconsistent results!");
            }
        }
    }

    public synchronized final int size() {
        return size;
    }

    public final void set(String key, Bitmap bimap) {
        if (key == null || bimap == null) {
            throw new NullPointerException("key == null || bitmap == null");
        } else {
            synchronized (this) {
                ++this.putCount;
                this.size += Utils.getBitmapBytes(bimap);
                Bitmap previous = this.map.put(key, bimap);
                if (previous != null) {
                    this.size -= Utils.getBitmapBytes(previous);
                }
            }
            this.trimToSize(this.maxSize);
        }

    }

    public final boolean contains(String key) {
        if (key == null) {
            throw new NullPointerException("key == null");
        }
        synchronized (this) {
            return this.map.containsKey(key);
        }
    }

    public synchronized final int maxSize() {
        return maxSize;
    }

    public final Bitmap get(String key) {
        if (key == null) {
            throw new NullPointerException("key == null");
        }

        synchronized (this) {
            Bitmap bitmap = this.map.get(key);
            if (bitmap != null) {
                ++this.hitCount;
                return bitmap;
            } else {
                ++this.missCount;
                return null;
            }
        }
    }

    public final void clearKeyUri(String keyPrefix) {
        synchronized (this) {
            int length = keyPrefix.length();
            Iterator<Map.Entry<String, Bitmap>> iterator = this.map.entrySet().iterator();
            boolean sizeChanged = false;
            while (true) {
                if (!iterator.hasNext()) {
                    if (sizeChanged) {
                        this.trimToSize(this.maxSize);
                    }
                    return;
                }

                Map.Entry<String, Bitmap> entry = iterator.next();
                String key = entry.getKey();
                Bitmap value = entry.getValue();
                int newlineIndex = key.indexOf(10);
                if (newlineIndex == length && key.substring(0, newlineIndex).equals(keyPrefix)) {
                    iterator.remove();
                    this.size -= Utils.getBitmapBytes(((Bitmap) value));
                    sizeChanged = true;
                }
                continue;
            }
        }
    }
}

